package org.wanho.rent.filters;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.wanho.rent.constant.Constant;
import org.wanho.rent.util.CommonUtil;

/**
 * 项目启动监听器
 * @author 
 * @date  下午2:51:20
 * @version 1.0
 */
@WebListener
public class ApplicationLisenter implements ServletContextListener {
	
	Timer timer;
	public static final long ONE_SECOND = 1000;
	
	@Override
	public void contextDestroyed(ServletContextEvent context) {
		CommonUtil.setContext(null);
		timer.cancel();//取消任务
		timer = null;
	}

	@Override
	public void contextInitialized(ServletContextEvent context) {
		ServletContext application = context.getServletContext();
		CommonUtil.setContext(application);
		String contextPath = CommonUtil.getContextPath();
		application.setAttribute(Constant.FILE_DIR_KEY, contextPath);
		System.out.println("获取文件请求路径=" + contextPath);
		
		timer = new Timer();
		TimerTask task = new MyTask();
		//开始任务
		//timer.schedule(task, 5 * ONE_SECOND, 60 * ONE_SECOND);
	}

}
